// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "UberViewer.h"
#include "UberViewerModelBar.h"
#include "MainFrm.h"

#include "ogr_spatialref.h"
#include "ogr_srs_api.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CUberViewerDocBar

BEGIN_MESSAGE_MAP(CUberViewerDocBar, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_NOTIFY(NM_CLICK, IDC_MODELLIST, OnChangeModelSelection)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUberViewerDocBar construction/destruction

CUberViewerDocBar::CUberViewerDocBar()
{
	TRACE("CUberViewerDocBar::CUberViewerDocBar");

	// TODO: add one-time construction code here
}

CUberViewerDocBar::~CUberViewerDocBar()
{
	TRACE("CUberViewerDocBar::~CUberViewerDocBar");

}

/////////////////////////////////////////////////////////////////////////////
// CUberViewerDocBar message handlers

int CUberViewerDocBar::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	TRACE("CUberViewerDocBar::OnCreate");
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	CRect rectDummy;
	rectDummy.SetRectEmpty ();
	this->GetWindowRect(rectDummy);

    // Create the splitter window
	if (!m_wndModelList.Create(WS_VISIBLE | WS_CHILD | LVS_LIST | LVS_SHOWSELALWAYS, rectDummy, this, IDC_MODELLIST))
        return false;

	return 0;
}


void CUberViewerDocBar::ShowDoc(CUberViewerDoc *doc)
{
	TRACE("CUberViewerDocBar::ShowDoc");
	m_curDoc = doc;
	m_wndModelList.DeleteAllItems();

	if (doc && !doc->m_instantiatedModels.empty())
	{
		int n = 0;
		std::list<InstantiatedModel*>::iterator it = doc->m_instantiatedModels.begin();
		for ( ; it != doc->m_instantiatedModels.end(); it++)
		{
			m_wndModelList.InsertItem(n, _T((*it)->pModel->m_fileName));
			m_wndModelList.SetItemData(n, (DWORD_PTR) *it);
			n++;
		}
	}
	((CMainFrame*) GetParentFrame())->UpdateModelPanes(NULL, NULL);
}


void CUberViewerDocBar::OnSize(UINT nType, int cx, int cy) 
{
	TRACE("CUberViewerDocBar::OnSize");

	CDockablePane::OnSize(nType, cx, cy);

	// Tab control should cover a whole client area:
	m_wndModelList.SetWindowPos (NULL, 0, 0, cx, cy, SWP_NOACTIVATE | SWP_NOZORDER);
}


void CUberViewerDocBar::OnChangeModelSelection(NMHDR* pNMHDR, LRESULT* pResult)
{
	TRACE("CUberViewerDocBar::OnChangeModelSelection");

	POSITION pos = m_wndModelList.GetFirstSelectedItemPosition();

	if (pos == NULL)
		((CMainFrame*) GetParentFrame())->UpdateModelPanes(NULL, NULL);

	while (pos)
	{
		int nItem = m_wndModelList.GetNextSelectedItem(pos);
		((CMainFrame*) GetParentFrame())->UpdateModelPanes(m_curDoc, (InstantiatedModel*) m_wndModelList.GetItemData(nItem));
	}

}
